# 导入py2neo库
from py2neo import Graph

# 连接到本地Neo4j数据库
graph = Graph("bolt://localhost:7687", auth=("neo4j", "12345678"))

# 查询所有的索引
indexes = graph.run("SHOW INDEXES").data()
for idx in indexes:
    if "Person" in str(idx.get("labelsOrTypes", "")) and "name" in str(
        idx.get("properties", "")
    ):
        index_name = idx["name"]
        print(f"删除索引:{index_name}")
        graph.run(f"DROP INDEX {index_name}")
# 创建索引
graph.run("CREATE INDEX person_name_index FOR (p:Person) ON (p.name)")

# 查询所有的约束
constraints = graph.run("SHOW CONSTRAINTS").data()
for constraint in constraints:
    if "Person" in str(constraint.get("labelsOrTypes", "")) and "email" in str(
        constraint.get("properties", "")
    ):
        constraint_name = constraint["name"]
        print("constraint_name", constraint_name)
        print(f"删除约束:{constraint_name}")
        graph.run(f"DROP CONSTRAINT {constraint_name}")
# 创建约束
graph.run(
    "CREATE CONSTRAINT person_email_unique FOR (p:Person)  REQUIRE p.email IS UNIQUE"
)
